var config = require("config");
// var ByteArray = require("ByteArray");
var Protocol = require("Protocol");
var EventHelper = require("EventHelper");
var PlayerDetailModel = require("PlayerDetailModel");
var dialogManager = require("dialogManager");
var MD5 = require("md5");
var GameNetMgr = require("GameNetMgr");

function initMgr(){
    cc.vv = {};
    cc.vv.http = require("HTTP");
    cc.vv.net = require("GameNet");

    var AudioMgr = require("AudioMgr");
    cc.vv.audioMgr = new AudioMgr();
    cc.vv.audioMgr.init();
}
   
cc.Class({
    extends: cc.Component,
    properties: {
        labLoading : {
            default : null,
            type : cc.Label
        },
        progress : {
            default : null,
            type : cc.Label
        },
    },
    // use this for initialization
    onLoad: function () {
        initMgr();
        this.progress.string = "";
        this.shareUid = 0;
        if(typeof(wx)=="undefined"){
            this.getServerInfo();
        }else{
            this.weixinLogin();
        }
        //设置常驻节点
        var myNode = new cc.Node("myNode");
        EventHelper.addPersistRootNode(myNode);
        this.isShow = false;

        console.log("typeof(wx):"+typeof(wx));
        if(typeof(wx)=="undefined"){return;}

        wx.setKeepScreenOn({
            keepScreenOn:true,
            success(res){
                console.log("设置保持常亮状态成功。",res);
            },
            fail(res){
                console.log("设置保持常亮状态失败。",res);
            }
        });

        wx.showShareMenu({
            withShareTicket: true,
            success(res){
                // console.log(">>>>>>>>>showShareMenu");
            }
        });

        var shareImg = config.getShareImgPath("task");
        wx.onShareAppMessage(function(res){
            return {
                title: config.shareTxt["task"][config.shareIndex],
                imageUrl: shareImg,
                query :  "key="+PlayerDetailModel.uid,
                success(res){
                    console.log("转发成功!!!");
                    // console.log(res);
                    GameNetMgr.sendRequest("System","ShareWxRes",1);
                },
                fail(res){
                    console.log("转发失败!!!")
                } 
            }
        });

        //获取系统信息
        wx.getSystemInfo({
            success(res){
                console.log("获取系统信息成功。",res);
                config.SystemInfo = res;
                config.setDevice(res.system);
            },
            fail(res){
                console.log("获取系统信息失败。",res);
            }
        });
    },
    onDestroy(){
        console.log("appStart Destroy");
        var self = this;
        EventHelper.RemoveCustomEvent(config.MyNode,"Regist",self.onRegist,self);
        EventHelper.RemoveCustomEvent(config.MyNode,"HeartBeat",self.onHeartBeat,self);
        EventHelper.RemoveCustomEvent(config.MyNode,"LoginOK",self.onLoginOK,self); 
    },
    weixinLogin(){
        var self = this;
        wx.login({
            success: function(ress) {
                if (ress.code) {
                    var _url = "https://sdk.youjoy.tv/Api/ThirdParty/WxSmallProgram/Login";
                    var params = {};
                    params.game_id = 1034;
                    params.version = "1.0.0";
                    params.code = ress.code;//game_id=1034
                    var sign = "code=" + ress.code + "&" + "game_id=" + "1034" + "&" + "version="+ "1.0.0";
                    console.log("sign:"+sign);
                    var _sign = self.getSign(sign);
                    params._sign = _sign.toUpperCase();

                    var xhr = null;
                    var complete = false;
                    var fnRequest = function(){
                        // console.log("sdk.youjoy.tv 正在连接服务器...");
                        xhr = cc.vv.http.sendRequest("",params,function(ret){
                            xhr = null;
                            complete = true;
                            // console.log("connectSdkServer = " + JSON.stringify(ret));
                            self.sdkuid = ret.data.user_id;
                            config.SDKUID = ret.data.user_id;
                            console.log("------------sdkuid:",self.sdkuid);
                            self.getServerInfo();
                        },_url);
                        setTimeout(fn,5000);            
                    };
                    
                    var fn = function(){
                        if(!complete){
                            if(xhr){
                                xhr.abort();
                                console.log("sdk.youjoy.tv 连接失败，即将重试...");
                                setTimeout(function(){
                                    fnRequest();
                                },5000);
                            }
                            else{
                                fnRequest();
                            }
                        }
                    };
                    fn();
                }else{
                  console.log('登录失败！' + res.errMsg)
                }
            }
        });
    },
    getSign(sign){
        var endData = MD5.hex_md5(sign+"79fad6f7510d86aabe01a7006153ddd0");
        return endData;
    },
    getServerInfo:function(){
        var params = {};
        params.env = "prod";
        params.ver = config.VERSION_NAME;
        params.channel = "weichatgame";
        params.udid = "udid";
        params.pver = config.VERSION_NAME;
        params.pchannel = "weichatgame";
        params.presver = config.VERSION_NAME;
        
        var self = this;
        var xhr = null;
        var complete = false;
        var fnRequest = function(){
            // console.log("正在连接服务器...");
            self.labLoading.string = "正在连接服务器...";
            xhr = cc.vv.http.sendRequest("",params,function(ret){
                // console.log("连接成功");
                if(self.labLoading)
                    self.labLoading.string = "连接成功...";
                xhr = null;
                complete = true;
                config.GlobalRouterUpdate(ret);
                console.log(config.GlobalRouter);
                self.connectGameServer();
            },config.GlobalRouter.director);
            setTimeout(fn,5000);            
        };
        
        var fn = function(){
            if(!complete){
                if(xhr){
                    xhr.abort();
                    // console.log("连接失败，即将重试...");
                    if(self.labLoading)
                        self.labLoading.string = "连接失败，即将重试...";
                    setTimeout(function(){
                        fnRequest();
                    },5000);
                }
                else{
                    fnRequest();
                }
            }
        };
        fn();
    },
    connectGameServer(){
        var self = this;
        cc.vv.net.connect(config.GlobalRouter.host,config.GlobalRouter.port,function(ret){
            //WebSocket连接成功
            if(typeof(wx)=="undefined"){
                self.sendRegist();
            }else{
                self.getWxUserInfo();
            }
        });

        var ConnectionClosedShow = function(){
            self.isShow = true;
            // dialogManager.showCommonDialog("提示","与服务器断开连接！",function(){
            //     self.isShow = false;
            //     cc.vv.audioMgr.stopMusic();
            //     cc.director.loadScene("LoadingScene");
            // });
            cc.vv.audioMgr.stopMusic();
            config.stopOnMassage = false;
            cc.director.loadScene("LoadingScene");
        }

        setInterval(function(){
            if(cc.vv.net.isPinging){
                self.sendHeartBeat();
            }else{
                if(!self.isShow)
                    ConnectionClosedShow();
            }     
        }.bind(this),10000);

        setInterval(function(){
            if(config.adCdTime > 0){
                config.adCdTime -= 1;
            }   
        }.bind(this),1000);

        EventHelper.AddCustomEvent(config.MyNode,"Regist",self.onRegist,self);
        EventHelper.AddCustomEvent(config.MyNode,"HeartBeat",self.onHeartBeat,self);
        EventHelper.AddCustomEvent(config.MyNode,"LoginOK",self.onLoginOK,self); 
    },
    getWxUserInfo(){
        var self = this;
        var launch = wx.getLaunchOptionsSync();
        console.log("launch =  " + JSON.stringify(launch));
        self.shareUid =  launch.query.key;
        console.log("shareUid =  " + self.shareUid);

        wx.getUserInfo({
            success: function (res) {
               console.log("res.userInfo = " + JSON.stringify(res.userInfo));
               config.UpdateWxInfo(res.userInfo);
               self.sendRegist();
             },
             fail: function (res){
                console.log("获取信息失败!!!")
                dialogManager.showAuthorizeDialog("授权失败","请您重新授权",null);
            } 
        });
    },
    onRegist(event){
        var self = this;
        console.log("Regist---");
        var data = event.getUserData();
        console.log(data)
        config.temppassword = data.data.data.password;
        self.sendLogin();
    },
    onHeartBeat(event){
        // console.log("HeartBeat---");
    },
    onLoginOK(event){
        var self = this;
        console.log("LoginOK---");
        console.log(event.getUserData());
        var data = event.getUserData();
        self.setPlayerDetailModel(data);
    },
    sendHeartBeat(){
        //发送心跳
        var params = {};
        params.t = Protocol.Request.HeartBeat.Alive;
        cc.vv.net.send("HeartBeat",Protocol.Command.HeartBeat,params);
        config.shareIndex = config.getRandom(1);
    },
    sendRegist(){
        //注册请求
        if(typeof(wx)=="undefined"){
            var params = {};
            params.t = Protocol.Request.Login.Regist;
            params.d = MD5.hex_md5("yepat123");
            params.u = "50ee8041a7fd8baa6348252ea114432bb3be23bd";
            params.v = config.VERSION_NAME;
            params.c = "weichatgame";
            params.e = "defaults";
            params.type = "guest";
            // params.sdkid = self.sdkuid;
            cc.vv.net.send("Regist",Protocol.Command.Login,params);
            return;
        }
        var self = this;
        var username = "3rdplt_" + self.sdkuid;
        var password = username + config.OpenUDIDEncryptToken + "suffix";
        password = MD5.hex_md5(password);
        var encodedUsername = username + config.OpenUDIDEncryptToken;
        encodedUsername = MD5.hex_md5(encodedUsername);
        var encodedUDID = self.sdkuid + config.OpenUDIDEncryptToken
        encodedUDID = MD5.hex_md5(encodedUDID);

        var params = {};
        params.t = Protocol.Request.Login.Regist;
        params.d = encodedUsername;
        params.a = username;
        params.p = password;
        params.v = config.VERSION_NAME;
        params.c = "weichatgame";
        params.e = encodedUDID;
        params.type = "third";
        params.sdkid = self.sdkuid;
        cc.vv.net.send("Regist",Protocol.Command.Login,params);  
    },
    sendLogin(){
        if(typeof(wx)=="undefined"){
            var params = {};
            params.t = Protocol.Request.Login.Guest;
            params.d = MD5.hex_md5("yepat123");
            params.p = config.temppassword;
            params.c = "weichatgame";
            params.v = config.VERSION_NAME;
            params.tv = 1;
            params.e = "defaults";
            params.wn = config.wxInfo.nickName;
            params.wgender = config.wxInfo.gender;
            params.wurl = config.wxInfo.avatarUrl;
            params.weichatgame_version = config.VERSION_ABOUT;//显示版本
            cc.vv.net.send("Login",Protocol.Command.Login,params); 
            return;
        }
        var self = this;
        var params = {};
        params.t = Protocol.Request.Login.Guest;
        params.d = self.sdkuid;
        params.p = config.temppassword;
        params.c = "weichatgame";
        params.v = config.VERSION_NAME;
        params.tv = 1;
        params.e = "defaults";
        params.f = self.sdkuid;
        params.wn = config.wxInfo.nickName;
        params.wgender = config.wxInfo.gender;//性别 0：未知、1：男、2：女
        params.wurl = config.wxInfo.avatarUrl;
        params.invite = self.shareUid;
        params.weichatgame_version = config.VERSION_ABOUT;//显示版本
        cc.vv.net.send("Login",Protocol.Command.Login,params);  
        // console.log(">>>>>>wgender:",config.wxInfo.gender)
    },
    setPlayerDetailModel(response){
        var room = response.data.room
        // console.log(room);
        if(response["code"] == Protocol.Response.Login.OK){
            var isDevData = response["data"]["isDev"];
            if (isDevData && typeof(isDevData) == "number"){
            }else{
                isDevData = 0
            }
            for(var i=0;i<room.length;i++){
                if(room[i].rooms){
                    for(var j=0;j<room[i].rooms.length;j++){
                        room[i].rooms[j].modelId = room[i].modelId;
                    }
                }
            }
            config.playGameId = response["data"].playGameId;
            config.playGameMsg = response["data"].playGameMsg;

            // console.log(">>>>>>>>>>>>>>>",response["data"].today_first_login)
            config.firstLogin = response["data"].today_first_login;

            PlayerDetailModel.nickname = response["data"]["nick"];
            PlayerDetailModel.uid = parseInt(response["data"]["uid"]);
            PlayerDetailModel.coin = parseInt(response["data"]["coins"]);
            PlayerDetailModel.gold = parseInt(response["data"]["gold"]);
            PlayerDetailModel.gender = parseInt(response["data"]["sex"]);
            PlayerDetailModel.word = response["data"]["word"];
            PlayerDetailModel.coolnum = response["data"]["cool_num"];
            PlayerDetailModel.level = parseInt(response["data"]["level"]);
            PlayerDetailModel.matches = parseInt(response["data"]["gameData"]["matches"]);
            PlayerDetailModel.win = parseInt(response["data"]["gameData"]["win"]);
            PlayerDetailModel.checkCode = response["data"]["check_code"];
            PlayerDetailModel.propDress = response["data"]["propDress"];
            PlayerDetailModel.score = parseInt(response["data"]["score"]);

            PlayerDetailModel.verfile = parseInt(response["data"]["verfile"]);
            PlayerDetailModel.vertips = parseInt(response["data"]["vertips"]);
            PlayerDetailModel.mailUnread = parseInt(response["data"]["mail_unread"]);

            PlayerDetailModel.taskUnReward = parseInt(response["data"]["weichatgame_task_unaward"]);
            PlayerDetailModel.shareUnReward = parseInt(response["data"]["weichatgame_invite_unaward"]);

            PlayerDetailModel.coupon = parseInt(response["data"]["coupon"]);
            PlayerDetailModel.age = parseInt(response["data"]["age"]);
            PlayerDetailModel.room = room;
            PlayerDetailModel.contact = response["data"]["contact"];
            PlayerDetailModel.contacts = response["data"]["contacts"];
            PlayerDetailModel.wechat = response["data"]["wechat"];

            PlayerDetailModel.lottery = parseInt(response["data"]["lottery"]);
            PlayerDetailModel.checkinUndo = parseInt(response["data"]["checkin_undo"]);
            PlayerDetailModel.task1Unaward = parseInt(response["data"]["task1_unaward"]);
            PlayerDetailModel.task2Unaward = parseInt(response["data"]["task2_unaward"]);
            PlayerDetailModel.chargeRate = parseInt(response["data"]["charge_rate"]);
            PlayerDetailModel.propItems = response["data"]["propItems"];

            PlayerDetailModel.rewardList = response["data"]["n_reward_list"];

            PlayerDetailModel.wechatImg = response["data"]["img"];
            PlayerDetailModel.title = response["data"]["title"];
            PlayerDetailModel.isDev = isDevData;

            // if(response["data"]["continueRoomId"])
            PlayerDetailModel.continueRoomId = response["data"]["continueRoomId"];

            console.log(response["data"]);

            //看广告cd
            config.adCdTime = response["data"]["watch_advertisement_cd"];
            config.showAd = response["data"]["watch_advertisement_open"];
            config.canSeeVideoAd = response["data"]["weichatgame_can_watch_advertisement"];
            console.log("config.canSeeVideoAd:",config.canSeeVideoAd);


            // console.log("-----config.showAd:",config.showAd);
            config.TrialShareShow = response["data"]["weichatgame_trial_share_show"];
            // console.log("-----config.TrialShareShow:",config.TrialShareShow);

            console.log("进入游戏大厅...uid:"+parseInt(response["data"]["uid"]));
            this.labLoading.string = "进入游戏大厅...";
            // this.preloadNextScene();
            this.downloadFile();
        }else{
            // console.log("登陆游戏失败！");
            this.labLoading.string = "登陆游戏失败！";
            dialogManager.showCommonDialog("提示","登陆游戏失败！",function(){
                cc.director.loadScene("LoadingScene");
            });
        }
    },
     //
     preloadNextScene(){
        cc.director.preloadScene("HallScene", function () {
            // cc.log("Next scene preloaded");
            cc.director.loadScene("HallScene");
        });
    },
    //下载资源包
    downloadFile(){
        var self = this;
        if(typeof(wx)=="undefined"){
            self.preloadNextScene();
        }else{

            var fileManager = wx.getFileSystemManager();
            var soundspath = wx.env.USER_DATA_PATH+"/ddz_res/1.0.6";
            console.log("soundspath:"+soundspath);
            fileManager.access({
                path:soundspath,
                success(res){
                    console.log("目录存在!!!",res);
                    self.preloadNextScene();
                },
                fail(res){
                    console.log("目录不存在!!!",res);
                    var downloadTask = wx.downloadFile({
                        url: 'https://sg.youjoy.tv/ddzwechatgame/resources/ddz_res_106.zip',
                        success: function(res) {
                            console.log("资源下载成功");
                            var filePath = res.tempFilePath;
                            fileManager.unzip({
                                zipFilePath:filePath,
                                targetPath:wx.env.USER_DATA_PATH,
                                success(res){
                                    console.log("解压成功!!!",res)
                                    self.preloadNextScene();
                                },
                                fail(res){
                                    console.log("解压失败!!!",res)
                                } 
                            });
                        },
                        fail(res){
                            console.log("资源下载失败",res);
                        } 
                    });
                    downloadTask.onProgressUpdate((res) => {
                        self.progress.string = res.progress+"%";
                        self.labLoading.string = "资源更新中...";
                        // console.log('下载进度', res.progress);
                        // console.log('已经下载的数据长度', res.totalBytesWritten);
                        // console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite);
                    })
                            
                } 
            });  
        }
    }


});
